package com.toKnow.service.impl;

import com.toKnow.mapper.*;
import com.toKnow.model.entity.*;
import com.toKnow.service.ResourcePubService;
import org.springframework.aop.scope.ScopedProxyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("resourcePubService")
public class ResourcePubServiceImpl implements ResourcePubService {

    @Autowired
    private ResourcePubDao resourcePubDao;
    @Autowired
    private ResourceTagPubDao resourceTagPubDao;
    @Autowired
    private ResourceTypePubDao resourceTypePubDao;
    @Autowired
    private UserInfoPubDao userInfoPubDao;
    @Autowired
    private ResourceCommentPubDao resourceCommentPubDao;
    @Autowired
    private ResourceCommentAnswerDao resourceCommentAnswerDao;
    @Autowired
    private ResourceLikePubDao resourceLikePubDao;
    @Autowired
    private ResourceCommentLikeDao resourceCommentLikeDao;
    @Autowired
    private ResourceAnswerLikePubDao resourceAnswerLikePubDao;

    public void setResourcePubDao(ResourcePubDao resourcePubDao) {
        this.resourcePubDao = resourcePubDao;
    }

    public void setResourceTagPubDao(ResourceTagPubDao resourceTagPubDao) {
        this.resourceTagPubDao = resourceTagPubDao;
    }

    public void setResourceTypePubDao(ResourceTypePubDao resourceTypePubDao) {
        this.resourceTypePubDao = resourceTypePubDao;
    }

    public void setUserInfoPubDao(UserInfoPubDao userInfoPubDao) {
        this.userInfoPubDao = userInfoPubDao;
    }

    public void setResourceCommentPubDao(ResourceCommentPubDao resourceCommentPubDao) {
        this.resourceCommentPubDao = resourceCommentPubDao;
    }

    public void setResourceCommentAnswerDao(ResourceCommentAnswerDao resourceCommentAnswerDao) {
        this.resourceCommentAnswerDao = resourceCommentAnswerDao;
    }

    public void setResourceLikePubDao(ResourceLikePubDao resourceLikePubDao) {
        this.resourceLikePubDao = resourceLikePubDao;
    }

    public void setResourceCommentLikeDao(ResourceCommentLikeDao resourceCommentLikeDao) {
        this.resourceCommentLikeDao = resourceCommentLikeDao;
    }

    public void setResourceAnswerLikePubDao(ResourceAnswerLikePubDao resourceAnswerLikePubDao) {
        this.resourceAnswerLikePubDao = resourceAnswerLikePubDao;
    }

    @Override
    @Transactional
    public Integer addResource(ResourcePub resourcePub,  UserInfoPub userInfoPub) {

        resourcePubDao.insertResourceReturnId(resourcePub);

        userInfoPubDao.updateResourceNum(userInfoPub);

        for(Integer tagId : resourcePub.getTagIds()) {
            resourceTagPubDao.insertResourceTag(new ResourceTagPub(resourcePub.getId(), tagId));
        }

        for(Integer typeId: resourcePub.getTypeIds()) {
            resourceTypePubDao.insertResourceType(new ResourceTypePub(resourcePub.getId(), typeId));
        }

        return resourcePub.getId();
    }

    @Override
    @Transactional
    public Integer storeResource(ResourcePub resourcePub,  UserInfoPub userInfoPub) {

        resourcePubDao.insertResourceDraftReturnId(resourcePub);

        for(Integer tagId : resourcePub.getTagIds()) {
            resourceTagPubDao.insertResourceDraftTag(new ResourceTagPub(resourcePub.getId(), tagId));
        }

        for(Integer typeId: resourcePub.getTypeIds()) {
            resourceTypePubDao.insertResourceDraftType(new ResourceTypePub(resourcePub.getId(), typeId));
        }

        return resourcePub.getId();
    }

    @Override
    @Transactional
    public Integer updateResourceDraft(Integer draftId, ResourcePub resourcePub,  UserInfoPub userInfoPub) {
        resourceTagPubDao.deleteResourceDraftTag(draftId);
        resourceTypePubDao.deleteResourceDarftType(draftId);

        resourcePub.setId(draftId);

        resourcePubDao.updateSourceDraft(resourcePub);

        for(Integer tagId : resourcePub.getTagIds()) {
            resourceTagPubDao.insertResourceDraftTag(new ResourceTagPub(resourcePub.getId(), tagId));
        }

        for(Integer typeId: resourcePub.getTypeIds()) {
            resourceTypePubDao.insertResourceDraftType(new ResourceTypePub(resourcePub.getId(), typeId));
        }

        return resourcePub.getId();
    }

    @Override
    @Transactional
    public int addSourceTag(ResourceTagPub resourceTagPub){

        int i = resourceTagPubDao.insertResourceTag(resourceTagPub);

        return i;
    }

    @Override
    @Transactional
    public int storeSourceTag(ResourceTagPub resourceTagPub){

        int i = resourceTagPubDao.insertResourceDraftTag(resourceTagPub);

        return i;
    }

    @Override
    @Transactional
    public int addSourceType(ResourceTypePub resourceTypePub){

        int i = resourceTypePubDao.insertResourceType(resourceTypePub);
        return i;

    }

    @Override
    @Transactional
    public int storeSourceType(ResourceTypePub resourceTypePub){

        int i = resourceTypePubDao.insertResourceDraftType(resourceTypePub);
        return i;

    }

    @Override
    @Transactional
    public int commentResource(ResourceCommentPub resourceCommentPub, UserInfoPub userInfoPub, Integer resource_id) {

        //System.out.println("service");

        int row1 =  resourceCommentPubDao.insertResourceComment(resourceCommentPub);
        //System.out.println("row1 = " + row1);

        int row2 =  userInfoPubDao.updateCommentTimes(userInfoPub);
       // System.out.println("row2 = " + row2);

        int row3 =  resourcePubDao.addResourceComment(resource_id);
        //System.out.println("row3 = " + row3);


        return (row1&row2&row3);

    }

    @Override
    @Transactional
    public int answerResourceComment(ResourceCommentAnswerPub resourceCommentAnswerPub) {

        //System.out.println("service");

        int row1 = resourceCommentAnswerDao.insertResourceAnswer(resourceCommentAnswerPub);

        return row1;
    }


    @Override
    @Transactional
    public int likeResource(ResourceLikePub resourceLikePub, Integer user_id, Integer resource_id) {

        //System.out.println("service");
        Integer count = resourceLikePubDao.countLike(resourceLikePub);
        System.out.println("count = " + count);

        if(count == 0){
            int row1 = resourceLikePubDao.insertResourceLike(resourceLikePub);
            System.out.println("row1 = " + row1 );

            int row2 = userInfoPubDao.updateLikeTimes(user_id);
            System.out.println("row2 = " + row2 );

            int row3 = resourcePubDao.addResourceLike(resource_id);
            System.out.println("row3 = " + row3 );

            return 1;
        }else {
            int row1 = resourceLikePubDao.deleteResourceLike(resourceLikePub);
            System.out.println("row1 = " + row1 );

            int row2 = userInfoPubDao.subLikeTimes(user_id);
            System.out.println("row2 = " + row2 );

            int row3 = resourcePubDao.subResourceLike(resource_id);
            System.out.println("row3 = " + row3 );

            return 2;
        }


    }

    @Override
    @Transactional
    public int likeComment(ResourceCommentLikePub resourceCommentLikePub, Integer comment_id) {

        //System.out.println("service");

        Integer count = resourceCommentLikeDao.countLike(resourceCommentLikePub) ;

        System.out.println("count = " + count);

        if(count == 0 ){

            int row1 = resourceCommentLikeDao.insertCommentLike(resourceCommentLikePub);
            System.out.println("row1 = " + row1 );

            int row2 = resourceCommentPubDao.addCommentLike(comment_id);
            System.out.println("row2 = " + row2 );

            return 1;
        }else {
            int row1 = resourceCommentLikeDao.deleteCommentLike(resourceCommentLikePub);
            System.out.println("row1 = " + row1 );

            int row2 = resourceCommentPubDao.subCommentLike(comment_id);
            System.out.println("row2 = " + row2 );

            return 2;
        }



    }

    @Override
    @Transactional
    public int likeAnswer(ResourceAnswerLikePub resourceAnswerLikePub, Integer answer_id) {

        //System.out.println("service");

        Integer count = resourceAnswerLikePubDao.countLike(resourceAnswerLikePub);
        System.out.println("count = " + count);

        if(count == 0 ){
            int row1 = resourceAnswerLikePubDao.insertAnswerLike(resourceAnswerLikePub);
            System.out.println("row1 = "+ row1);

            int row2 = resourceCommentAnswerDao.addAnswerLike(answer_id);
            System.out.println("row2 = "+ row2);

            return 1;

        }else {
            int row1 = resourceAnswerLikePubDao.deleteAnswerLike(resourceAnswerLikePub);
            System.out.println("row1 = "+ row1);

            int row2 = resourceCommentAnswerDao.subAnswerLike(answer_id);
            System.out.println("row2 = "+ row2);

            return 2;
        }


    }


    @Override
    public Integer findAuthor(Integer source_id) {
        Integer author_id = resourcePubDao.selectAuthor(source_id);
        return author_id;
    }
}
